const bodyParser = require("body-parser");
const express = require('express');
const app = express();
const port = 3000;

const RankImgUtil = require("./rank-img-util");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req, res) => {
  res.send('Please ust post request of /getRankImg');
});

app.post('/api/getRankImg', function (req, res) {
    try{
        let param = req.body;
        let rankList = param.rankList || [];
        let groupTitle = param.groupTitle || "未知群组";
        let matchTitle = param.matchTitle || "未知标题";
        let createTime = param.createTime || "2022-01-01 00:00:01";
        let themeOption = param.themeOption || {};
        let bottomText = param.bottomText || "";
        let promise = RankImgUtil.createScoreImgBase64(rankList, groupTitle, matchTitle, createTime, themeOption, bottomText);
        promise.then(imgBase64=>{
            let result = new Object();
            result.imgBase64 = imgBase64;
            result.error = 0;
            res.send(JSON.stringify(result));
        });
    }catch{
        let result = new Object();
        result.error = 1002;
        result.msg = "create img error";
        res.send(JSON.stringify(result));
    }
});


app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Headers', 'mytoken');
    next();
});

app.listen(port, () => {
});